package com.qyhl.webtv.module_user.login.wx.binding;

import com.google.gson.Gson;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.entity.user.UserBean;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.module_user.app.UserApplication;
import com.qyhl.webtv.module_user.common.UserUrl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class BindingPresenterModel implements BindingContract.BindingPresenterModel {
    private BindingContract.BindingView view;

    public BindingPresenterModel(BindingContract.BindingView view) {
        this.view = view;
    }

    @Override
    public void getCode(String phone) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.GET_USER_GETCODE);

            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "siteId=" + CommonUtils.getInstance().getSite() + "&type=4&telphone=" + phone))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            view.getCodeError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(String s) {
                            ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                            if (apiResult.getCode() == 200) {
                                view.getCodeSuccess();
                            } else {
                                view.getCodeError("获取验证码失败!");
                            }
                        }
                    });
        } else {
            view.getCodeError("请检查网络...");
        }
    }

    @Override
    public void gobinding(String openid, String name, String phone, String code, String invitation, String gender, String iconurl) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(UserUrl.WXBINDING);
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                            "openid=" + openid + "&siteId=" + CommonUtils.getInstance().getSite() + "&telphone=" + phone +
                                    "&captcha=" + code + "&nickname=" + name + "&sex=" + gender +
                                    "&logo=" + iconurl + "&inviteCode=" + invitation))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .execute(new SimpleCallBack<UserBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.bindingError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(UserBean userBean) {
                            CommonUtils.getInstance().putConfigData(AppConfigConstant.LOGIN_TOKEN,userBean.getToken());
                            Hawk.put("token", userBean.getToken());
                            getUserInfo(userBean.getUsername());
                        }
                    });

        } else {
            view.bindingError("请检查网络...");
        }
    }

    /**
     * 获取用户信息
     *
     * @param username
     */
    private void getUserInfo(String username) {
        if (NetUtil.isNetworkAvalible(UserApplication.getApp())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String time = sdf.format(new Date());
            String code = "Z5" + time + "I2863";
            String url = UserUrl.GET_USER_INFO_NEW + "?timestamp=" + time;

            EasyHttp.post(url)
                    .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&username=" + username))
                    .isDecrypt(true)
                    .decryptKet(code)
                    .execute(new SimpleCallBack<UserInfoBean>() {
                        @Override
                        public void onError(ApiException e) {
                            view.bindingError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(UserInfoBean userInfoBean) {
                            view.bindingSuccess(userInfoBean);
                        }
                    });
        } else {
            view.bindingError("请检查网络...");
        }
    }
}
